package gui.services;

import gui.dao.CategoryDAO;
import gui.dao.RecordDAO;
import gui.entity.Category;
import gui.entity.Record;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author:feixiong
 * @Date: 2021/1/25
 */
public class CategoryService {
    CategoryDAO cdao=new CategoryDAO();
    RecordDAO rdao=new RecordDAO();
    public List<Category> list() throws Exception {
        List<Category> cs = cdao.list();
        for (Category c:cs){
            List<Record> rs=rdao.list();
            c.setRecordNumber(rs.size());
        }
        Collections.sort(cs,(c1,c2)->c2.getRecordNumber()-c1.getRecordNumber());
        return cs;
    }
    public void add(String name) throws Exception {
        Category c=new Category();
        c.setName(name);
        cdao.add(c);
    }
    public void update(int id, String name) throws Exception {
        Category c = new Category();
        c.setName(name);
        c.setId(id);
        cdao.update(c);
    }

    public void delete(int id) throws Exception {
        cdao.delete(id);
    }
}
